package ca.uwindsor.cs.deepweb.utility.documentanalysis;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Sort the term frequency pair by its frequency in ascending order
 * @author Liang Jie
 *
 */
public class TermsLoader {
	
	protected File termslist;
	
	private ArrayList<TermFrequencyCell> list;
	
	public TermsLoader(){
		list = new ArrayList<TermFrequencyCell>();
	}
	
	public void load(String file){
		InputStreamReader reader = null;
		BufferedReader bufferedreader_in;
		String string_line = new String();
		String[] temp;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			bufferedreader_in = new BufferedReader(reader);
			string_line = bufferedreader_in.readLine();
			while (string_line != null ) {
				if(!string_line.equals("")){
					temp=string_line.split("\t");
					TermFrequencyCell cell = new TermFrequencyCell(temp[0], Integer.parseInt(temp[1]));
					list.add(cell);
				}
				string_line = bufferedreader_in.readLine();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		Collections.sort(list);
		
	}
	
	public void outputList(String file) throws UnsupportedEncodingException, IOException{
		FileOutputStream fp = new FileOutputStream(file);
		StringBuffer buf = new StringBuffer();
		for(TermFrequencyCell c : list){
			buf.append(c.getTerm());
			buf.append(c.getFrequency());
			buf.append("\n");
		}
		fp.write(buf.toString().getBytes("UTF-8"));
		fp.flush();
		fp.close();
	}

	/**
	 * @return the list
	 */
	public ArrayList<TermFrequencyCell> getList() {
		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		args = new String[2];
		args[0]="/var/research/estimation/experiment/documentanalysis/corrected/gov2_0_terms_of_digits_5digit_with_ocurrence_sorted.txt";
		args[1]="/var/research/estimation/experiment/documentanalysis/corrected/gov2_0_terms_of_digits_5digit_sorted.txt";
		
		if(args.length!=2){
			System.out.println("java TermsLoader wordfrequencyfile outputwordfile");
			System.exit(0);
		}
		TermsLoader tl = new TermsLoader();
		tl.load(args[0]);
		try {
			tl.outputList(args[1]);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
